// Copyright (c) 1996-2002 John Lyon-Smith. All rights reserved.

#pragma once

#include <wxtl\SyncObject.h>

namespace wxtl
{
	const LONGLONG kOneSecondOf100Nanoseconds = 10000000;
	const LONGLONG kOneSecondOfMilliseconds = 1000;

	class Timer : public SyncObject
	{
	public:
		Timer()
		{
		}
		Timer(Timer &timer) throw() :
			SyncObject(timer)
		{
		}
		explicit Timer(HANDLE hTimer) throw() :
			SyncObject(hTimer)
		{
		}
		~Timer()
		{
		}
		Timer& operator=(Timer& h) throw()
		{
			return (Timer&)Handle::operator=(h);
		}

		bool create(const TCHAR* pszName = NULL, bool bManualReset = false, LPSECURITY_ATTRIBUTES psa = NULL)
		{
			_ASSERTE(!isValid());
			return ((m_h = ::CreateWaitableTimer(psa, bManualReset, pszName)) != NULL);
		}
		
		bool setRelative(LONGLONG llNanoSecondDelay, LONG lMillisecondPeriod = 0)
		{
			_ASSERTE(isValid());

			llNanoSecondDelay = -llNanoSecondDelay;

			if (!SetWaitableTimer(m_h, 
				reinterpret_cast<LARGE_INTEGER*>(&llNanoSecondDelay), 
				lMillisecondPeriod, NULL, NULL, FALSE))
			{
				close();
				return false;
			}

			return true;
		}
		
		void Cancel()
		{
			_ASSERTE(isValid());
			CancelWaitableTimer(m_h);
		}
	};
}

